<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_approval_pumk extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('keu_pumk_model', 'UMK');
		$this->load->model('keu_umk_detail_model', 'UMKDetail');
		$this->load->model('combo_model', 'Combo');
		$this->load->model('keu_coa_model', 'Coa');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Approval Pembuatan UMK";
		$data['data'] = null;
		$data['data']['date_from'] = date('01/m/Y');
		$data['data']['date_to'] = date('t/m/Y');
		
		viewByTemplate('keu_approval_pumk/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->UMK->GetJSON();
	}
	
	
	function edit()
	{
		$data['title'] = "Approval Pembuatan UMK";
		$data['data'] = $this->UMK->GetSingleView(get('id_umk'));
				
		$data['office_data']		= json_encode($this->Combo->SWOffice());
		$data['valuta_data']		= json_encode($this->Combo->SWValuta());
		$data['cost_center_data']	= json_encode($this->Combo->SWCostCenter());
		$data['jenis_umk_data']		= json_encode($this->UMK->KeuJenisPUMK());
		
		$data['mode'] = "edit";
		viewByTemplate('keu_approval_pumk/form', $data, 'form');
	}

	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		
		
		if (post("jml_dana_disetujui") == '') {
			$error->Add("jml_dana_disetujui", "Jumlah Dana Disetujui", $tpl_empty);
		}
		
		if (post("ket_approve") == '') {
			$error->Add("ket_approve", "Keterangan Approval", $tpl_empty);
		}
		
		return $error;
	}
	
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('id_umk', 'tgl_approve',  'ket_approve', 'jml_dana_disetujui', 'status_approve'));
				
				
				
				
				
				if (post("mode") == "create")
				{
				
				}
				else if (post("mode") == "edit")
				{
				
					$data['tgl_approve']		= date_format3(post('tgl_approve'), 'YYYY-MM-DD');
					$data['jml_dana_disetujui']	= toNumber(post('jml_dana_disetujui'));
					$data['modify_date']		= NOW;
					$data['modify_user']		= USERID;
					
					$this->UMK->Update($data, 
										array('id_umk'	=> $data['id_umk']));
					
					/*
					$data_det['dana_disetujui']		= $data['jml_dana_disetujui'];
					$data_det['create_date']		= NOW;
					$data_det['create_user']		= USERID;

					
					$this->UMKDetail->Update($data_det, 
										array('id_umk'	=> $data['id_umk']));
					*/
					
					$ls_response = "Data Tersimpan";
					
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
				
				// Detail Payment
				$dtl_umk		= $this->data['dtl_umk'];
				$total_deleted	= $dtl_umk['num_row']['deleted'];
				$total_inserted	= $dtl_umk['num_row']['inserted'];
				$total_updated	= $dtl_umk['num_row']['updated'];
				for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
					$row = $dtl_umk['deleted'][$i];
					$this->UMKDetail->DeleteDetailUMK($data['id_umk'], $row['no_umk']);
				}
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					$row = $dtl_umk['inserted'][$i];
					
					$row['jml_unit_diajukan']	= toNumber($row['jml_unit_diajukan'])=='NaN' ? 0 : toNumber($row['jml_unit_diajukan']);
					$row['harga_satuan']		= toNumber($row['harga_satuan'])=='NaN' ? 0 : toNumber($row['harga_satuan']);
					$row['dana_diajukan']		= toNumber($row['dana_diajukan'])=='NaN' ? 0 : toNumber($row['dana_diajukan']);
					$row['id_umk'] 				= $data['id_umk'];
					$row['no_umk']				= $this->get_data('SELECT KEU_UMK_DETAIL_SEQ.NEXTVAL FROM DUAL');
					
					$row['jml_unit_disetujui']	= toNumber($row['jml_unit_disetujui'])=='NaN' ? 0 : toNumber($row['jml_unit_disetujui']);
					$row['dana_disetujui']		= toNumber($row['dana_disetujui'])=='NaN' ? 0 : toNumber($row['dana_disetujui']);
					
					$row['jml_unit_pertanggungjwb']	= $row['jml_unit_disetujui'];
					$row['dana_pertanggungjwb']		= $row['dana_disetujui'];
					$row['harga_real_satuan']		= $row['harga_satuan'];
					
					$row['create_date']	= NOW;
					$row['create_user']	= USERID;
					
					$this->UMKDetail->Add($row);
				}
				for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
					$row = $dtl_umk['updated'][$i];
					$row['jml_unit_diajukan']	= toNumber($row['jml_unit_diajukan'])=='NaN' ? 0 : toNumber($row['jml_unit_diajukan']);
					$row['harga_satuan']		= toNumber($row['harga_satuan'])=='NaN' ? 0 : toNumber($row['harga_satuan']);
					$row['dana_diajukan']		= toNumber($row['dana_diajukan'])=='NaN' ? 0 : toNumber($row['dana_diajukan']);
					$row['id_umk'] 				= $data['id_umk'];
					
					$row['jml_unit_disetujui']	= toNumber($row['jml_unit_disetujui'])=='NaN' ? 0 : toNumber($row['jml_unit_disetujui']);
					$row['dana_disetujui']		= toNumber($row['dana_disetujui'])=='NaN' ? 0 : toNumber($row['dana_disetujui']);
					
					$row['jml_unit_pertanggungjwb']	= $row['jml_unit_disetujui'];
					$row['dana_pertanggungjwb']		= $row['dana_disetujui'];
					$row['harga_real_satuan']		= $row['harga_satuan'];
					
					$row['modify_date']	= NOW;
					$row['modify_user']	= USERID;
				
				
					$this->UMKDetail->Update($row, array(	'id_umk'	=> $row['id_umk'], 
															'no_umk'	=> $row['no_umk'] ));
				}
				
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$id_umk	= get('id_umk');
		
		if ($id_umk != "")
		{
			// Delete Single
			$ls_result = $this->UMK->DeleteSingle($id_umk);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				$ls_result .= $this->UMK->DeleteSingle($row['id_umk']);
			}
		}
		
		echo $ls_result;
	}
	
	function dtl_umk_data()
	{
		echo $this->UMKDetail->GetJSON();
	}
	
}
?>